const fs = require('fs')
const path = require('path')
const fse = require('fs-extra');
const { exec } = require('child_process');
const { logger } = require('./logger');

let assates_path = "../assets/resources/gl_zh/res/db";


function filterFiles(dir, ext, recur = true) {
    let files = [];

    let temp = fs.readdirSync(dir);
    temp.forEach(_f => {
        let _stat = fs.statSync(dir + path.sep + _f);
        if (_stat.isDirectory()) {
            files.push(...filterFiles(dir + path.sep + _f, ext, recur));
        } else {
            if (!ext || _f.endsWith(ext)) {
                files.push(dir + path.sep + _f);
            }
        }
    })
    return files;
}

function removeSync(src) {
    if (!fse.existsSync(src)) return;

    console.log(`remove ${src}`);
    fse.removeSync(src);
}

let files = filterFiles(assates_path, ".meta");

function removeMeta(files_arr) {
    files_arr.forEach((src) => {
        removeSync(src);
    });
}

removeMeta(files);

exec('7z a -tzip -r ' + assates_path + ".zip" + " " + assates_path, {
    maxBuffer: 1024 * 1024 * 1024,
}, (error, stdout, stderr) => {
    if (error) {
        logger.error(`cocos 编译失败`, error);
        return;
    }
    if (stderr) {
        logger.error(`cocos 编译失败`, stderr);
        return;
    }
    logger.log(`db 压缩成功`);
});


